package com.example.store;

import android.content.Context;

import com.tencent.mmkv.MMKV;
import com.tencent.mmkv.MMKVHandler;
import com.tencent.mmkv.MMKVLogLevel;
import com.tencent.mmkv.MMKVRecoverStrategic;
import com.zy.zlog.ZLog;

import java.util.Arrays;

public class MMKVManager {

    private static final String TAG = "MMKV";

    private static final MMKVManager mmkv = new MMKVManager();

    private MMKVManager() {

    }

    public static MMKVManager getInstance() {
        return mmkv;
    }

    public void init(Context context) {
        String rootDir = MMKV.initialize(context);
        ZLog.d(TAG, "mmkv root: " + rootDir);
    }

    public void testAdd() {
        MMKV kv = MMKV.defaultMMKV();
        kv.encode("bool", true);
        ZLog.d(TAG, "bool: " + kv.decodeBool("bool"));

        kv.encode("int", Integer.MIN_VALUE);
        ZLog.d(TAG, "int: " + kv.decodeInt("int"));

        kv.encode("long", Long.MAX_VALUE);
        ZLog.d(TAG, "long: " + kv.decodeLong("long"));

        kv.encode("float", -3.14f);
        ZLog.d(TAG, "float: " + kv.decodeFloat("float"));

        kv.encode("double", Double.MIN_VALUE);
        ZLog.d(TAG, "double: " + kv.decodeDouble("double"));

        kv.encode("string", "Hello from mmkv");
        ZLog.d(TAG, "string: " + kv.decodeString("string"));

        byte[] bytes = {'m', 'm', 'k', 'v'};
        kv.encode("bytes", bytes);
        ZLog.d(TAG, "bytes: " + new String(kv.decodeBytes("bytes")));
    }

    //删除 & 查询：
    public void testRemove() {
        MMKV kv = MMKV.defaultMMKV();

        kv.removeValueForKey("bool");
        ZLog.d(TAG, "bool: " + kv.decodeBool("bool"));


        kv.removeValuesForKeys(new String[]{"int", "long"});

        ZLog.d(TAG, "allKeys: " + Arrays.toString(kv.allKeys()));

        boolean hasBool = kv.containsKey("bool");
        ZLog.d(TAG, "hasBool: " + hasBool);
    }


    //如果不同业务需要区别存储，也可以单独创建自己的实例：
    public void testID() {
        MMKV kv = MMKV.mmkvWithID("MyID01");
        kv.encode("bool", true);
        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));


        kv = MMKV.mmkvWithID("MyID02");
        kv.encode("bool", true);
        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));


        kv = MMKV.mmkvWithID("MyID03");
        kv.encode("bool", true);
        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));
    }

    public void testMultiProcessDode() {
        MMKV kv = MMKV.mmkvWithID("InterProcessKVID", MMKV.MULTI_PROCESS_MODE);
        kv.encode("bool", true);

        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));
    }

    public void testLogcat() {
        MMKV.registerHandler(new MMKVHandler() {
            @Override
            public MMKVRecoverStrategic onMMKVCRCCheckFail(String mmapID) {
                return null;
            }

            @Override
            public MMKVRecoverStrategic onMMKVFileLengthError(String mmapID) {
                return null;
            }

            @Override
            public boolean wantLogRedirecting() {
                return true;
            }

            @Override
            public void mmkvLog(MMKVLogLevel level, String file, int line, String function, String message) {
                ZLog.d(TAG, level + " " + file + " " + line + " " + function + " " + message);
            }
        });
    }


    int logLevel = -1;

    public void testLogcatEnable() {
        logLevel++;
        MMKVLogLevel[] values = MMKVLogLevel.values();
        MMKV.setLogLevel(values[logLevel % values.length]);
    }

    public void testEncrypt() {
        String cryptKey = "My-Encrypt-Key";
        MMKV kv = MMKV.mmkvWithID("EncryptID", MMKV.SINGLE_PROCESS_MODE, cryptKey);

        ZLog.d(TAG, kv);
        kv.encode("bool", true);
        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));
    }

    //你可以更改密钥，也可以将一个加密 MMKV 改成明文，或者反过来。
    public void testEncryptOpt1() {
        final String mmapID = "testAES_reKey1";

        // an unencrypted MMKV instance
        MMKV kv = MMKV.mmkvWithID(mmapID, MMKV.SINGLE_PROCESS_MODE, null);

        // change from unencrypted to encrypted
        kv.reKey("Key_seq_1");


        // change encryption key
        kv.reKey("Key_seq_2");


        // change from encrypted to unencrypted
        kv.reKey(null);

        ZLog.d(TAG, kv);
        kv.encode("bool", true);
        ZLog.d(TAG, "kv: " + kv + " " + Arrays.toString(kv.allKeys()));
    }

    public void testDIYRoot(Context context) {
        String dir = context.getFilesDir().getAbsolutePath() + "/mmkv_2";
        String rootDir = MMKV.initialize(dir);
        ///data/user/0/com.example.ipcdemo/files/mmkv_2
        ZLog.d(TAG, "mmkv root: " + rootDir);
    }

    public void testDIYRoot02(Context context) {
        String relativePath = context.getFilesDir().getAbsolutePath() + "/mmkv_3";
        MMKV kv = MMKV.mmkvWithID("testCustomDir", relativePath);
        ZLog.d(TAG, "mmkv root: " + kv);
        //2023-07-14 15:42:54.135 28025-28025 MMKV                    com.example.ipcdemo                  I  <MMKV_Android.cpp:156::mmkvWithID> prepare to load testCustomDir (id 21669a6ebed72d4d06f3782b3a36299c) from rootPath /data/user/0/com.example.ipcdemo/files/mmkv_3
        //2023-07-14 15:42:54.135 28025-28025 MMKV                    com.example.ipcdemo                  I  <MemoryFile.cpp:97::open> open fd[0x34], /data/user/0/com.example.ipcdemo/files/mmkv_3/21669a6ebed72d4d06f3782b3a36299c
        //2023-07-14 15:42:54.135 28025-28025 MMKV                    com.example.ipcdemo                  I  <MemoryFile.cpp:97::open> open fd[0x3a], /data/user/0/com.example.ipcdemo/files/mmkv_3/21669a6ebed72d4d06f3782b3a36299c.crc
        //2023-07-14 15:42:54.135 28025-28025 MMKV                    com.example.ipcdemo                  I  <MMKV_IO.cpp:78::loadFromFile> loading [21669a6ebed72d4d06f3782b3a36299c] with 0 actual size, file size 4096, InterProcess 0, meta info version:0
        //2023-07-14 15:42:54.135 28025-28025 MMKV                    com.example.ipcdemo                  I  <MMKV_IO.cpp:127::loadFromFile> loaded [21669a6ebed72d4d06f3782b3a36299c] with 0 key-values
        //2023-07-14 15:42:54.135 28025-28025 ZYDebug                 com.example.ipcdemo                  D  MMKV mmkv root: com.tencent.mmkv.MMKV@11400a3
    }
}
